package com.spring.mvc.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

/**
 * MD5加密工具
 * @author liujian
 *
 */
public class MD5Util {
	public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	public static String compute(String str) throws Exception {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		char[] charArray = str.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; ++i)
			byteArray[i] = (byte) charArray[i];

		byte[] md5Bytes = md5.digest(byteArray);

		StringBuffer hexValue = new StringBuffer();

		for (int i = 0; i < md5Bytes.length; ++i) {
			int val = md5Bytes[i] & 0xFF;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString().toUpperCase();
	}

	public static String getHash(String fileName) throws Exception {
		int count = 0;
		InputStream fis = new FileInputStream(fileName);
		byte[] buffer = new byte[1024];
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		int numRead = 0;
		while ((numRead = fis.read(buffer)) > 0) {
			md5.update(buffer, 0, numRead);
			++count;
		}

		fis.close();
		return toHexString(md5.digest());
	}

	public static String toHexString(byte[] b) {
		StringBuffer sb = new StringBuffer(b.length * 2);

		for (int i = 0; i < b.length; ++i) {
			System.out.print(b[i]+",");
			sb.append(hexChar[((b[i] & 0xF0) >>> 4)]);
			sb.append(hexChar[(b[i] & 0xF)]);
		}
		System.out.println();
		return sb.toString();
	}
/*01010111
 * 
10101001

00001111
00001001


1010*/

	public static void main(String[] args) throws Exception {
		String code=MD5Util.getHash("config/properties/propmt.properties");
		System.out.println(code.toUpperCase());
		System.out.println(Integer.toHexString((-87&0xF0)>>4));
		System.out.println(Integer.toHexString(7&0xFF).toUpperCase());
		System.out.println(Integer.toHexString(-2&0xff));
		
		
		System.out.println(Integer.toHexString(2));
		System.out.println(Integer.toHexString(15));
		System.out.println(Integer.toHexString(16));
		System.out.println(Integer.valueOf("F",16));
		
		
		System.out.println(Integer.toHexString(-2));
		System.out.println(Integer.toHexString(-2&0xff));
		System.out.println(Integer.toHexString(-2&0x000000ff));
		
		 //注，FE输出时不会为-2，因为此时不会把FE看成负数，valueOf会把所有数字串看成正的   
		System.out.println(Integer.valueOf("FE",16));
		//如果要输出-2，只能按以下形式输出   
		System.out.println(Integer.valueOf("-2",16));
		 //所以要把 FE 看成负的话，只能在前面加上负号，但是这里输出还不是-2，   
        //而是先计算Integer.valueOf("FE", 16)，再在结果前加上负   
        System.out.println(Integer.valueOf("-FE", 16));//-254   
        
        System.out.println(((long)1<<31));
        System.out.println(0x7fffffff+1);
        System.out.println(-0x80000000);
        
        System.out.println(Integer.toBinaryString(-2));
        System.out.println(Integer.toBinaryString(-2&0xff));
        System.out.println(Integer.toBinaryString(-2&0x000000ff));
        
        
        String s="testasdfasdtaet";
        MessageDigest md=MessageDigest.getInstance("MD5");
        byte bt[]=md.digest(s.getBytes());
        for (int i = 0; i < bt.length; i++) {
			System.out.print(bt[i]+" ");
		}
        System.out.println();
        StringBuilder sb=new StringBuilder(bt.length*2);
        for (int i = 0; i < bt.length; i++) {
			sb.append(hexChar[(bt[i]&0xF0)>>4]);
			sb.append(hexChar[bt[i]&0xF]);
		}
        System.out.println(sb.toString().toUpperCase());
        
        long l=0;
        byte[] b={1,2,3,4,5,6,7,23,4,5,6,7,7,8,8,5,45,4,2,2,2,34,34,45,45,45,46,56};
        for (int i = 0; i < b.length; i++) {
			l=l<<1|1;
		}
        System.out.println(l);
        System.out.println(Long.bitCount(l));
        System.out.println();
        
        System.out.println((byte)(2257>>>24));
        System.out.println((byte)(2257>>>16));
        System.out.println((byte)(2257>>>8));
        System.out.println((byte)(2257>>>0));
        
        System.out.println((8&0xFF)<<8);
        System.out.println((-47&0xFF)<<0);
	}
}
//0000 0100
//1111 1011
//1111 1100
